package com.cyandpos.pad.login.model;


import android.text.TextUtils;

import com.cyandpos.pad.common.commondata.BaseResponse;
import com.cyandpos.pad.common.netmanager.PosHttpManager;
import com.cyandpos.pad.http.OkHttpUtils;
import com.cyandpos.pad.posdata.LoadConfig;
import com.cyandpos.pad.posdata.Machine;
import com.google.gson.JsonObject;

/**
 * Created by Yangzw on 2017/2/25.
 */
public class LoginModelImpl implements ILoginModel {
    @Override
    public void loadLoginConfig(Machine machine, String mac, final OnLoadLoginConfigListener listener) {

        PosHttpManager.LoginHttpEexcute(machine,mac,new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject response) {
                BaseResponse<LoadConfig> baseResponse = BaseResponse.fromJson(response.toString(),LoadConfig.class);
                if(baseResponse.getCode().equals("0")){
                    try {
                        listener.onLoadLoginConfigSuccess(baseResponse.getData());
                    }catch (Exception e){
                        listener.onLoadLoginConfigFailure(baseResponse.getCode() + baseResponse.getMsg() + e.getMessage().toString());
                    }
                }else{
                    if (TextUtils.isEmpty(baseResponse.getTips())){
                        listener.onLoadLoginConfigFailure(baseResponse.getMsg());
                    }else {
                        listener.onLoadLoginConfigFailure(baseResponse.getTips());
                    }
                }
            }
            @Override
            public void onFailure(String msg) {
                listener.onLoadLoginConfigFailure(msg);
            }
        });
    }


    public interface  OnLoadLoginConfigListener{
        void onLoadLoginConfigSuccess(LoadConfig loadConfig);
        void onLoadLoginConfigFailure(String errorMsg);
    }




}
